#ifndef RENDERER_H
#define RENDERER_H

#include <memory>

#include <QMutex>
#include <QMatrix4x4>

class QOpenGLContext;
class QThread;

class CameraInfo;

/**
 * @brief The Renderer class 渲染模型基类
 *
 * 功能：渲染各种模型
 * - 渲染逻辑
 *   - 创建并初始化渲染数据
 *   - 渲染
 *   - 释放资源
 *
 * 注：不管是否必要，析构时最好调用 release
 */
class Renderer
{
    enum FixedType
    {
        FixedType_Unfixed = 0,
        FixedType_FixedMatrix = 1,
        FixedType_FixedScale = 2
    };

public:
    Renderer();

    void requestCreate();
    void requestRender(const CameraInfo &camera_info);
    void requestRelease();

    /// 显隐
    virtual void setVisible(bool visiable);

    /// 固定视角
    virtual void setFixed(bool fixed,
                          const QMatrix4x4 &fixed_matrix = QMatrix4x4());
    virtual void setFixed(bool fixed, const QVector3D &scale);

protected:
    /// 需要确保 create 与 render, release 函数在同一个线程及同一个上下文内执行
    /// create 时记录，render, release 时判断是否相同
    virtual void create() = 0;
    virtual void render(const CameraInfo &camera_info) = 0;
    virtual void release() = 0;

private:
    bool beginCreate();
    bool beginRender();
    bool beginRelease();

private:
    QOpenGLContext *ctx_{ nullptr };
    QThread *thread_{ nullptr };
    QMutex mtx_;

    bool visiable_{ true };

    FixedType fixed_{ FixedType_Unfixed };
    QMatrix4x4 fixed_matrix_;
    QVector3D fixed_scale_;
};

#endif // RENDERER_H
